package com.sita.linboard.LinBoardDao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.sita.linboard.base.BaseApplication;

/**
 * Created by Jason on 2016/8/29.
 */
public class DaoSingleTon {
    private static final String DATABASE_NAME = "linboard.db";
    private volatile static DaoSingleTon instance;
    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private Context context;
    private DaoMaster.DevOpenHelper helper;
    private UserInfoDao userInfoDao;
    private DaoSingleTon(){
        context = BaseApplication.getContext();
    }
    public static DaoSingleTon getInstance(){
        if (instance ==null){
            synchronized (DaoSingleTon.class){
                instance = new DaoSingleTon();
            }
        }
        return instance;
    }
    public  DaoMaster.DevOpenHelper getHelper(){
        if (helper ==null){
            helper = new DaoMaster.DevOpenHelper(context,DATABASE_NAME,null);
        }
        return helper;
    }
    private SQLiteDatabase getDb(){
        if (db ==null){
            db = getHelper().getWritableDatabase();
        }
        return db;
    }
    private DaoMaster getDaoMaster(){
        if (daoMaster==null){
            daoMaster = new DaoMaster(getDb());
        }
        return daoMaster;
    }
    private DaoSession getDaoSession(){
        if (daoSession==null){
            daoSession = getDaoMaster().newSession();
        }
        return daoSession;
    }
    public UserInfoDao getUserInfoDao(){
        if (userInfoDao ==null){
            userInfoDao = getDaoSession().getUserInfoDao();
        }
        return userInfoDao;
    }
}
